package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.qo.EmployeeQueryObject;
import cn.wolfcode.crm.qo.PageResult;
import cn.wolfcode.crm.service.IEmployeeService;
import cn.wolfcode.crm.util.JSONResult;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    @RequestMapping("roleIds")
    @ResponseBody
    public Object selectPermissionById(Long employeeId) {
        //将数据转换成json格式
        Long[]ids = employeeService.selectRoleById(employeeId);


        return ids;
    }
    //验证用户名是否存在
    @RequestMapping("checkUsername")
    @ResponseBody
    public Object checkUsername(String username) {
        JSONResult result = new JSONResult();
        //将数据转换成json格式
        try {
            employeeService.checkUsername(username);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark1(e.getMessage());
        }


        return result;
    }

    @RequestMapping("view")
    public String list(){
        return "/employee/list";
    }

    //异步加载数据
    @RequestMapping("query")
    @ResponseBody
    public Object list(@ModelAttribute("qo") EmployeeQueryObject qo, Model model) {
        //将数据转换成json格式
        PageResult result = employeeService.queryForList(qo);

        return result;
    }


    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Employee entity){
        JSONResult result = new JSONResult();
        try {
            employeeService.saveOrUpdate(entity);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }
    @ResponseBody
    @RequestMapping("changeState")
    public Object changeState(Long id){
        employeeService.changeState(id);
        JSONResult json = new JSONResult();
        return json;
    }

    //数据导出

    @RequestMapping("exportData")
    public void exportData(HttpServletResponse response) throws IOException{
        //设置下载的响应头信息
        response.addHeader("Content-disposition","attachment;filename=employee.xls");
        //1.查询所有的员工信息
        List<Employee> employeeList = employeeService.selectAll();
        // 2.创建一个exel工作表 需要到poi的包
        HSSFWorkbook workbook = new HSSFWorkbook();
        //3.创建一个工作页签
        HSSFSheet sheet = workbook.createSheet();
        HSSFRow header = sheet.createRow(0);
        //4.创建单元格信息
        header.createCell(0).setCellValue("用户名");
        header.createCell(1).setCellValue("真实姓名");
        header.createCell(2).setCellValue("邮箱");
        header.createCell(3).setCellValue("电话号码");
        for (int i = 0; i < employeeList.size(); i ++){
            Employee employee = employeeList.get(i);
            HSSFRow currentRow = sheet.createRow(i + 1);
            //5.创建单元格信息
            currentRow.createCell(0).setCellValue(employee.getUsername());
            currentRow.createCell(1).setCellValue(employee.getRealname());
            currentRow.createCell(2).setCellValue(employee.getEmail());
            currentRow.createCell(3).setCellValue(employee.getTel());
        }
        workbook.write(response.getOutputStream());
        //关闭流操作
        workbook.close();
    }

    //数据导入
    @ResponseBody
    @RequestMapping("importData")
    public JSONResult importData(MultipartFile file) throws IOException{
        JSONResult jsonResult = new JSONResult();
        //1.创建一个excel工作表
        HSSFWorkbook workbook = new HSSFWorkbook(file.getInputStream());
        //2.获取第一个页签
        HSSFSheet sheet = workbook.getSheetAt(0);
        //3.获取到这个sheet中的总行数
        int num = sheet.getLastRowNum();
        for (int i = 1; i <= num; i ++){
            HSSFRow currentRow = sheet.getRow(i);
            String username = currentRow.getCell(0).getStringCellValue();
            String realname = currentRow.getCell(1).getStringCellValue();
            String email = currentRow.getCell(2).getStringCellValue();
            String tel = currentRow.getCell(3).getStringCellValue();
            Employee employee = new Employee();
            employee.setUsername(username);
            employee.setRealname(realname);
            employee.setEmail(email);
            employee.setTel(tel);
            employee.setPassword("1");
            employee.setAdmin(false);
            employee.setState(true);
            employeeService.saveOrUpdate(employee);
        }
        //关闭流操作
        workbook.close();
        return jsonResult;
    }

}
